<template>
<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-row>
                <a-col :span="12">
                    <a-form-item label="订单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.odd" placeholder="请输入订单号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="采购日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请选择采购日期" style="width: 100%" v-decorator="['purchaseDate']" :trigger-change="true" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-search type="list" v-decorator="['supplier',validatorRules.supplier]" :trigger-change="true" dictCode="bas_supplier,name,id" @sendNameId="getNameId" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['contact',validatorRules.contact]" placeholder="请输入联系人"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['contactPhone',validatorRules.phone]" placeholder="请输入联系电话"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="交货日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请选择交货日期" v-decorator="['deliveryDate',validatorRules.deliveryDate]" :trigger-change="true" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="业务员" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['salesman']" placeholder="请输入业务员"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="是否中止执行" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-tag type="list" v-model="dataList.isSuspendExecution" :trigger-change="true" dictCode="yn" @sendName="getName" />
                    </a-form-item>
                </a-col>
                <a-col :span="23" :offset="1">
                    <a-form-item label="摘要" :labelCol="{ span: 2 }" :wrapperCol="{ span: 21 }">
                        <a-input v-decorator="['digest']" placeholder="请输入摘要"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="23">
                    <a-form-item label="交货地址" class="text-center" :labelCol="{ span: 3 }" :wrapperCol="{ span: 21 }">
                        <a-input v-decorator="['deliveryAddress']" placeholder="请输入交货地址"></a-input>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-spin>
</j-modal>
</template>

<script>
import {
    httpAction
} from '@/api/manage'
import pick from 'lodash.pick'
import JDate from '@/components/jeecg/JDate'

export default {
    name: 'PurchaseOrderModal',
    components: {
        JDate,
    },
    props: {
        odd: {
            type: String,
            default: '',
        },
    },
    data() {
        return {
            form: this.$form.createForm(this),
            title: '操作',
            width: 800,
            visible: false,
            model: {},
            dataList: {
                odd: '',
                isSuspendExecution: '否',
            },
            labelCol: {
                sm: {
                    span: 6,
                },
            },
            wrapperCol: {
                sm: {
                    span: 16,
                },
            },
            confirmLoading: false,
            validatorRules: {
                supplier: {
                    rules: [{
                        required: true,
                        message: '请选择供应商!'
                    }]
                },
                contact: {
                    rules: [{
                        required: true,
                        message: '请输入联系人!'
                    }]
                },
                phone: {
                    rules: [{
                        required: true,
                        message: '请输入手机号!',
                    }]
                },
                deliveryDate: {
                    rules: [{
                        required: true,
                        message: '请输入交货日期!'
                    }]
                },
            },
            url: {
                add: '/procurement/purchaseOrder/add',
                edit: '/procurement/purchaseOrder/edit',
            },
        }
    },
    watch: {
        odd() {
            if (this.odd) {
                this.$set(this.dataList, 'odd', this.odd)
            }
        },
    },
    methods: {
        getName(value) {
            this.$set(this.dataList, 'isSuspendExecution', value)
        },
        getNameId(value) {
            this.$set(this.dataList, 'supplierId', value)
        },
        add() {
            this.edit({})
        },
        edit(record) {
            this.form.resetFields()
            this.dataList = {
                supplierId: record.supplierId,
                isSuspendExecution: record.isSuspendExecution ? record.isSuspendExecution : '否',
            }
            this.odd && (!record.supplierId) ? this.$set(this.dataList, 'odd', this.odd) : this.$set(this.dataList, 'odd', record.odd)
            this.model = Object.assign({}, record)
            this.visible = true
            this.$nextTick(() => {
                this.form.setFieldsValue(
                    pick(
                        this.model,
                        'purchaseDate',
                        'supplier',
                        'contact',
                        'contactPhone',
                        'deliveryDate',
                        'deliveryAddress',
                        'salesman',
                        'digest'
                    )
                )
            })
        },
        close() {
            this.$emit('close')
            this.visible = false
        },
        handleOk() {
            const that = this
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true
                    let httpurl = ''
                    let method = ''
                    if (!this.model.id) {
                        httpurl += this.url.add
                        method = 'post'
                    } else {
                        httpurl += this.url.edit
                        method = 'put'
                    }
                    //新增字段单独赋值
                    values = Object.assign(values, this.dataList)
                    let formData = Object.assign(this.model, values)
                    httpAction(httpurl, formData, method)
                        .then((res) => {
                            if (res.success) {
                                that.$message.success(res.message)
                                that.$emit('ok')
                            } else {
                                that.$message.warning(res.message)
                            }
                        })
                        .finally(() => {
                            that.confirmLoading = false
                            that.close()
                        })
                }
            })
        },
        handleCancel() {
            this.close()
        },
        popupCallback(row) {
            this.form.setFieldsValue(
                pick(
                    row,
                    'purchaseDate',
                    'supplier',
                    'contact',
                    'contactPhone',
                    'deliveryDate',
                    'deliveryAddress',
                    'salesman',
                    'digest'
                )
            )
        },
    },
}
</script>
